package persistentie;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class Connectie 
{
/*Attributen*/
	private static final String DATABASE_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
	private static final String DATABASE_URL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=rac";
	private static Connectie connectie;
	private Connection connection;
	
	
/*Constructor*/
	public static Connectie getInstance()
	{	
		if(connectie == null)
			connectie = new Connectie();
		return connectie;
	}
	private Connectie()
	{
		try
		{
			Class.forName( DATABASE_DRIVER ); //databasedriverklasse laden
			this.connection = DriverManager.getConnection( DATABASE_URL );//verbinding maken met de db
		}
		catch ( SQLException sqlException ) //queryfouten
		{
			JOptionPane.showMessageDialog( null, sqlException.getMessage(),"Database Error", JOptionPane.ERROR_MESSAGE );
		    System.exit( 1 );
		}
		catch ( ClassNotFoundException classNotFound ) //driverfouten
		{
			JOptionPane.showMessageDialog( null, classNotFound.getMessage(),"Driver Not Found", JOptionPane.ERROR_MESSAGE );
		    System.exit( 1 );
		}
	}
	
/*Methodes*/
	public Connection getConnection()
	{
		return connection;
	}
	public void closeConnection()
	{
		try
	    {
			connection.close();
			connectie = null;
	    }
	    catch ( SQLException sqlException )
	    {
	    	JOptionPane.showMessageDialog( null,sqlException.getMessage(), "Database Error",JOptionPane.ERROR_MESSAGE );
	        System.exit( 1 );
	    }
	}
}
